﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Z1029.NoteEditor
{
    public class CreaterCtrl : MonoBehaviour
    {
        public NoteSetter dld;

        //UI
        public Dropdown noteType;
        public Dropdown direction;
        public InputField speed;
        public InputField holdTime;
        public InputField posX;
        public Toggle autoPosX;
        public Toggle dianLiDe;
        public Toggle continuous;
        public Toggle attachToBeatline;

        public RectTransform re;

        private void Start()
        {
            UpdateData();
        }

        private void Update()
        {
            if (CanGenerateNote() && dld.mouseSetPos==2)
            {
                UpdateData();
            }
        }

        public void UpdateData()
        {
            noteType.value = (int)dld.targetSetType;
            direction.value = (int)dld.direction;
            speed.text = dld.Speed.ToString();
            holdTime.text = dld.holdTime.ToString();
            posX.text = dld.PosX.ToString();
            autoPosX.isOn = dld.mouseSetPos == 2;
            dianLiDe.isOn = dld.useMouse;
            continuous.isOn = dld.continuedGenerate;
            attachToBeatline.isOn = dld.attachToBeatline;
        }

        public void ChangeData()
        {
            dld.targetSetType = (NoteType)noteType.value;
            dld.direction = (Note.Direction)direction.value;
            dld.Speed = float.Parse(speed.text);
            dld.holdTime = int.Parse(holdTime.text);
            dld.PosX = float.Parse(posX.text);
            if (!autoPosX.isOn) dld.mouseSetPos = 1;
            if (autoPosX.isOn) dld.mouseSetPos = 2;
            else dld.mouseSetPos = 0;
            dld.useMouse = dianLiDe.isOn;
            dld.continuedGenerate = continuous.isOn;
            dld.attachToBeatline = attachToBeatline.isOn;
            UpdateData();
        }

        public bool CanGenerateNote()
        {
            return Input.mousePosition.x - RectToMouse(re).x < re.sizeDelta.x && Input.mousePosition.x - RectToMouse(re).x > 0 && ViewCtrl.viewType == ViewCtrl.ViewType.Hand;
        }

        public Vector2 RectToMouse(RectTransform rect)
        {
            Vector2 res = Vector2.zero;
            res.x = rect.anchoredPosition.x + Screen.width / 2 - rect.sizeDelta.x / 2;
            res.y = rect.anchoredPosition.y + Screen.height / 2 - rect.sizeDelta.y / 2;
            return res;
        }
    }
}